<script setup>
import { ref } from "vue";
import {
  listTreeCategories,
  level1Categories,
} from "@/api/mall/server/product/cate";
import {
  listBrand2Category,
  addBrand2Category,
  delBrand2Category,
} from "@/api/mall/server/product/relation";
const { proxy } = getCurrentInstance();
const isAddRelation = ref(false);

// 打开添加框
const openFormDialog = () => {
  isAddRelation.value = true;
};

// 获取关联列表
const loading = ref(false);
const props = defineProps({
  brandId: {
    type: Number,
    default: null,
  },
});
const queryParams = ref({
  pageSize: 10,
  pageNum: 1,
  brandId: props.brandId,
});
const total = ref(0);
const brand2CateList = ref([]);
const getBrand2CateList = async () => {
  loading.value = true;
  const res = await listBrand2Category(queryParams.value);
  brand2CateList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
// 获取分类列表
const categoryList = ref([]);
const getCategoryList = async () => {
  const res = await listTreeCategories();
  categoryList.value = res.data;
};

// 添加关联
const selectedCate = ref(null);
const addBrand2Cate = async () => {
  if (selectedCate.value) {
    const res = await addBrand2Category(props.brandId, selectedCate.value);
    isAddRelation.value = false;
    proxy.$modal.msgSuccess("添加关联成功！");
    await getBrand2CateList();
  }
};
// 删除关联
const deleteBrand2Cate = async (row) => {
  proxy.$modal
    .confirm(`是否删除关联？`)
    .then(() => {
      return delBrand2Category(row.id);
    })
    .then(() => {
      getBrand2CateList();
      proxy.$modal.msgSuccess("删除成功");
    });
};
getCategoryList();
getBrand2CateList();
</script>

<template>
  <div style="padding-bottom: 20px">
    <el-row class="row-container">
      <el-col>
        <!-- 分类 -->
        <el-popover
          placement="right"
          :visible="isAddRelation"
          :width="300"
          trigger="click"
        >
          <template #reference>
            <el-button type="primary" icon="Edit" @click="openFormDialog">
              关联分类
            </el-button>
          </template>
          <el-tree-select
            :data="categoryList"
            :props="{ value: 'catId', label: 'name', children: 'children' }"
            value-key="catId"
            v-model="selectedCate"
            placeholder="请选择关联分类"
            :teleported="false"
            filterable
          >
          </el-tree-select>
          <div style="text-align: right; margin: 0">
            <el-button size="small" text @click="isAddRelation = false"
              >取消</el-button
            >
            <el-button size="small" type="primary" @click="addBrand2Cate">
              确定
            </el-button>
          </div>
        </el-popover>
      </el-col>
    </el-row>
    <el-row class="row-container">
      <el-table :data="brand2CateList">
        <el-table-column label="品牌名" prop="brandName"></el-table-column>
        <el-table-column label="分类名" prop="catelogName"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button link type="primary" @click="deleteBrand2Cate(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getBrand2CateList"
      />
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
::v-deep .el-pagination__sizes {
  display: none;
}

::v-deep .el-pagination__jump {
  display: none;
}
</style>
